package com.joeyzh.utils;

import android.text.TextUtils;

import com.joey.base.util.SharedPreferenceUtils;

/**
 * Created by xuHuanLin on 2019/3/12.
 *
 * @author by xuHuanLin
 * @Description
 */

public class DbAppConsts {


    /**
     * 路径url常量类，url都在该类定义
     */
    public static final class Url {
        /**
         * 域名
         */
        private final static String IP_ADDR = "alpcontrol.runker.top";/*"192.168.137.1";*///"alpcontrol.runker.top";
        private final static String PORT = "2007";//"2007";//"13579";
        private final static String PROG_NAME = "";
        private static String DOMAIN_ROOT = "http://" + IP_ADDR + ":" + PORT + "/" + PROG_NAME + "/";

        /**
         * 数据库用户名
         */
        private final static String DB_USER = "root";
        /**
         * 数据库密码
         */
        private final static String DB_PWD = "root";
        /**
         * 数据库名称
         */
        private final static String DB_NAME = "alp_test";


        private static String DOMAIN_DB_ROOT = DB_USER + "/" + DB_PWD + "--" + DB_NAME;

        public static String setDbName(String username,String password ,String dbname){
            String username1 = DB_USER;
            String password1 = DB_PWD;
            String dbname1 = DB_NAME;
            if (username != null && !username.isEmpty()) {
                username1 = username;
            }
            if (password != null && !password.isEmpty()) {
                password1 = password;
            }
            if (dbname != null && !dbname.isEmpty()) {
                dbname1 = dbname;
            }

            SharedPreferenceUtils.getInstance().putString("username",username1);
            SharedPreferenceUtils.getInstance().putString("password",password1);
            SharedPreferenceUtils.getInstance().putString("dbName",dbname1);


            if (!TextUtils.isEmpty(dbname1)) {
                DOMAIN_DB_ROOT = String.format("%s/%s--%s", username1,password1,dbname1);
            } else {
                DOMAIN_DB_ROOT = String.format("%s/%s--%s", DB_USER, DB_PWD, DB_NAME);
            }

            return DOMAIN_DB_ROOT;
        }


        public static String setServer(String ip, String port, String progName) {
            String ip1 = IP_ADDR;
            String port1 = PORT;
            String progName1 = PROG_NAME;
            if (ip != null && !ip.isEmpty()) {
                ip1 = ip;
            }
            if (port != null && !port.isEmpty()) {
                port1 = port;
            }
            if (progName != null && !progName.isEmpty()) {
                progName1 = progName;
            }
            SharedPreferenceUtils.getInstance().putString("dbHost", ip1);
            SharedPreferenceUtils.getInstance().putString("dbPort", port1);
            SharedPreferenceUtils.getInstance().putString("dbServer", progName1);
            String temp;
            if (TextUtils.isEmpty(port1)) {
                temp = String.format("http://%s/", ip1);
            } else {
                temp = String.format("http://%s:%s/", ip1, port1);
            }
            if (!TextUtils.isEmpty(progName1)) {
                DOMAIN_ROOT = String.format("%s%s/", temp, progName1);
            } else {
                DOMAIN_ROOT = temp;
            }

            return DOMAIN_ROOT;
        }

        public static String getDomainRoot() {
            return DOMAIN_ROOT;
        }

        public static String resetServer() {
            return setServer(IP_ADDR, PORT, PROG_NAME);
        }


        public static String resetDBServer() {
            return setDbName(DB_USER, DB_PWD, DB_NAME);
        }
    }

}
